package org.lagou.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.ResumeDao;
import org.lagou.Dao.UserDao;
import org.lagou.Dao.impl.ResumeDaoImpl;
import org.lagou.Dao.impl.UserDaoImpl;
import org.lagou.bean.ReponseBody;
import org.lagou.bean.ResumeBean;
import org.lagou.bean.UserBean;
import org.lagou.tools.Tools;

import com.google.gson.Gson;

@WebServlet("/ResumeServlet")
public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ResumeServlet() {
		super();

	}

	Gson gson = new Gson();
	ResumeDao rdao = new ResumeDaoImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String t = request.getParameter("t");
		switch (t) {
		case "getResumeAll":
			getResumeAll(request, response);
			break;
		case "getResumePagin":
			getResumePagin(request, response);
			break;
		case "getResumeCount":
			getResumeCount(request, response);
			break;
		case "getResumeByUserid":
			getResumeByUserid(request, response);
			break;
		case "getResumeById":
			getResumeById(request, response);
			break;
		case "addResume":
			addResume(request, response);
			break;
		case "updateResume":
			updateResume(request, response);
			break;
		case "deleteResumeById":
			deleteResumeById(request, response);
			break;
		case "updateImg":
			updateImg(request, response);
			break;
		case "updateResumeName":
			updateResumeName(request, response);
			break;
		case "updateUserInformation":
			updateUserInformation(request, response);
			break;
		case "updateExpectWork":
			updateExpectWork(request, response);
			break;
		case "updateWorkExperience":
			updateWorkExperience(request, response);
			break;
		case "updateProjectExperience":
			updateProjectExperience(request, response);
			break;
		case "updateEducationBackground":
			updateEducationBackground(request, response);
			break;
		case "updateSelfDescription":
			updateSelfDescription(request, response);
			break;
		case "updateWorksShow":
			updateWorksShow(request, response);
			break;
		case "getResumeByResumeid":
			getResumeByResumeid(request, response);
			break;
		case "getResumeByState":
			getResumeByState(request, response);
			break;
		default:
			break;
		}
	}

	private void getResumeByState(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int state = Integer.parseInt(request.getParameter("state"));
		
		rb.setData(rdao.getResumeByState(userid, state));

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getResumeByResumeid(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		int id = -1;
		for (ResumeBean r : rlist) {
			id = r.getId();
		}

		rb.setData(rdao.getResumeByResumeid(id));

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getResumeById(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));

		if (!rdao.getResumeById(id).isEmpty()) {
			rb.setMsg("查询成功");
			rb.setData(rdao.getResumeById(id));
		} else {
			rb.setMsg("查询失败");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void updateWorksShow(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");

		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String url = request.getParameter("url");
		String rexplain = request.getParameter("workName");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(url, rexplain, date);
		if (rdao.updateWorksShow(resume, id) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改作品展示失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateSelfDescription(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String introduce = request.getParameter("myRemark");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(introduce, date);
		if (rdao.updateSelfDescription(resume, id) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改自我描述失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateEducationBackground(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String school = request.getParameter("schoolName");
		String education = request.getParameter("education");
		String major = request.getParameter("professional");
		String beginyear = request.getParameter("startYear");
		String endyear = request.getParameter("endYear");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(education, school, major, beginyear, endyear, date);
		if (rdao.updateEducationBackground(resume, id) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改教育背景失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateProjectExperience(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String project = request.getParameter("projectName");
		String role = request.getParameter("positionName");
		String pbeginyear = request.getParameter("startYear");
		String pbeginmonth = request.getParameter("startMonth");
		String pendyear = request.getParameter("endYear");
		String pendmonth = request.getParameter("endMonth");
		String rdescribe = request.getParameter("projectRemark");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(project, role, pbeginyear, pbeginmonth, pendyear, pendmonth, rdescribe,
				date);
		if (rdao.updateProjectExperience(resume, id) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改项目经验失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateWorkExperience(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String oldcompany = request.getParameter("companyName");
		String oldposition = request.getParameter("positionName");
		String cbeginyear = request.getParameter("startYear");
		String cbeginmonth = request.getParameter("startMonth");
		String cendyear = request.getParameter("endYear");
		String cendmonth = request.getParameter("endMonth");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(id, oldcompany, oldposition, cbeginyear, cbeginmonth, cendyear, cendmonth,
				date);
		if (rdao.updateWorkExperience(resume) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改工作经历失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateExpectWork(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String city = request.getParameter("city");
		String nature = request.getParameter("positionType");
		String rposition = request.getParameter("positionName");
		String salary = request.getParameter("salarys");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(id, city, nature, rposition, salary, date);
		if (rdao.updateExpectWork(resume) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改期望工作失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateUserInformation(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String education = request.getParameter("highestEducation");
		String edexperience = request.getParameter("workYear");
		String tel = request.getParameter("phone");
		String newemail = request.getParameter("email");
		String state = request.getParameter("status");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(id, name, sex, education, edexperience, tel, newemail, state, date);
		if (rdao.updateUserInformation(resume) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改基本信息失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateResumeName(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String resumename = request.getParameter("resumeName");

		ResumeBean resume = new ResumeBean(id, resumename);
		if (rdao.updateResumeName(resume) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改简历名失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateImg(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int id = 0;
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		for (ResumeBean r : rlist) {
			id = r.getId();
		}
		String img = request.getParameter("imgName");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(id, img, date);
		if (rdao.updateImg(resume) > 0) {
			rb.setMsg("修改成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改图片名失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateResume(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));

		String resumename = request.getParameter("resumename");
		int userid = Integer.parseInt(request.getParameter("userid"));
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String sex = request.getParameter("resumename");
		String education = request.getParameter("education");
		String edexperience = request.getParameter("edexperience");
		String tel = request.getParameter("tel");
		String state = request.getParameter("state");

		String img = request.getParameter("img");

		String city = request.getParameter("city");
		String nature = request.getParameter("nature");
		String position = request.getParameter("position");
		String salary = request.getParameter("salary");

		String oldcompany = request.getParameter("oldcompany");
		String oldposition = request.getParameter("oldposition");
		String cbeginyear = request.getParameter("cbeginyear");
		String cbeginmonth = request.getParameter("cbeginmonth");
		String cendyear = request.getParameter("cendyear");
		String cendmonth = request.getParameter("cendmonth");

		String project = request.getParameter("project");
		String role = request.getParameter("role");
		String pbeginyear = request.getParameter("pbeginyear");
		String pbeginmonth = request.getParameter("pbeginmonth");
		String pendyear = request.getParameter("pendyear");
		String pendmonth = request.getParameter("pendmonth");
		String rdescribe = request.getParameter("rdescribe");

		String school = request.getParameter("school");
		String major = request.getParameter("major");
		String beginyear = request.getParameter("beginyear");
		String endyear = request.getParameter("endyear");

		String introduce = request.getParameter("introduce");

		String url = request.getParameter("url");
		String rexplain = request.getParameter("rexplain");
		String date = Tools.getNowDate();

		ResumeBean resume = new ResumeBean(id, resumename, userid, name, sex, education, edexperience, tel, email,
				state, img, city, nature, position, salary, oldcompany, oldposition, cbeginyear, cbeginmonth, cendyear,
				cendmonth, project, role, pbeginyear, pbeginmonth, pendyear, pendmonth, rdescribe, school, major,
				beginyear, endyear, introduce, url, rexplain, date);
		if (rdao.updateResume(resume) > 0) {
			rb.setMsg("修改所有信息成功");
		} else {
			rb.setStatus(0);
			rb.setMsg("修改失败");
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getResumeByUserid(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");

		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		if (!ulist.isEmpty()) {
			rb.setMsg("查询成功");
			rb.setData(rdao.getResumeByUserid(userid));
		} else {
			rb.setMsg("您未登录");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void deleteResumeById(HttpServletRequest request, HttpServletResponse response) {

	}

	private void addResume(HttpServletRequest request, HttpServletResponse response) {

	}

	private void getResumeAll(HttpServletRequest request, HttpServletResponse response) {

	}

	private void getResumeCount(HttpServletRequest request, HttpServletResponse response) {

	}

	private void getResumePagin(HttpServletRequest request, HttpServletResponse response) {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
